#!/bin/bash

function help_msg {
    start_server="xrnserver-cli --start-server -stop-file /path/to/stopfile -input-tar input.tar -cache-dir /path/to/cache -output-tar output.tar -input-log-json input.json -output-log-json output.json"
    stop_server="xrnserver-cli --stop-server -stop-file /path/to/stopfile"
    echo ""
    echo "help page for the xrnserver-cli command"
    echo ""
    echo "usage:"
    echo ""
    echo "$start_server"
    echo ""
    echo "$stop_server"
    echo ""
}

if [ "$#" -eq 3 ]; then

    if [ "$1" == "--stop-server" ] && [ "$2" == "-stop-file" ] && [ ! -z "$3" ]; then

        touch $3
        exit 0
    else

        help_msg 
        exit 1
    fi
elif [ "$#" == "0" ]; then

    help_msg 
    exit 1
elif [ "$#" -ne 13 ]; then

    echo "error number of arguments $#"
    help_msg 
    exit 1
fi

input_tar=0
cachedir=0
output_tar=0
inputjson=0
outputjson=0
stopfile=0

for arg in "$@"; do
    if [ "$input_tar" == "1" ]; then
        input_tar=2
        input_tar_file=$arg
    fi
    if [ $arg == "-input-tar" ]; then
        input_tar=1
    fi

    if [ "$output_tar" == "1" ]; then
        output_tar=2
        output_tar_file=$arg
    fi
    if [ $arg == "-output-tar" ]; then
        output_tar=1
    fi

    if [ "$cachedir" == "1" ]; then
        cachedir=2
        cache_dir=$arg
    fi
    if [ $arg == "-cache-dir" ]; then
        cachedir=1
    fi

    if [ "$outputjson" == "1" ]; then
        outputjson=2
        outputjson_file=$arg
    fi
    if [ $arg == "-output-log-json" ]; then
        outputjson=1
    fi

    if [ "$inputjson" == "1" ]; then
        inputjson=2
        inputjson_file=$arg
    fi
    if [ $arg == "-input-log-json" ]; then
        inputjson=1
    fi

    if [ "$stopfile" == "1" ]; then
        stopfile=2
        stopfile_file=$arg
    fi
    if [ $arg == "-stop-file" ]; then
        stopfile=1
    fi

done

if [[ "$input_tar" != "2" || "$output_tar" != "2" || "$cachedir" != "2" || "$outputjson" != "2" || "$inputjson" != "2" || "$stopfile" != "2" ]]; then
    help_msg
    exit 0
fi

if [ ! -d "$cache_dir" ]; then
    echo "the $cache_dir directory does not exist"
    help_msg
    exit 0
fi

# check if input tar file exists
if [ ! -e "$input_tar_file" ]; then
    last_mod_time=""
else
    last_mod_time=$(stat -c %Y "$input_tar_file")
fi

current_dir=$(pwd)

while [ 1 ]; do

    # stop the server
    if [ -e $stopfile_file ]; then
        rm $stopfile_file
        exit 0
    fi

    # check if input tar file exists
    if [ ! -e "$input_tar_file" ]; then

        current_mod_time=""

    else

        current_mod_time=$(stat -c %Y "$input_tar_file")

    fi

    # wait until received tar
    if [[ "$current_mod_time" != "$last_mod_time" && -e "$input_tar_file" ]]; then

        # wait until file has been written
        while [ "$current_mod_time" != "$last_mod_time" ]; do
             last_mod_time=$current_mod_time
             if [ ! -e "$input_tar_file" ]; then
                 current_mod_time=""
             else
                 current_mod_time=$(stat -c %Y "$input_tar_file")
             fi
        done

        # clean cache
        cmd="rm -rf $cache_dir/*"
        echo "$cmd" ; eval "$cmd"

        # change directory to cache
        cmd="cd $cache_dir"
        echo "$cmd" ; eval "$cmd"

        # extract archive
        cmd="tar -xf $input_tar_file "
        echo "$cmd" ; eval "$cmd"

        # check inputs
        cmd="xrnarchive-cli --check-inputs -json-command command.json -json-log $inputjson_file"
        echo "$cmd" ; eval "$cmd"

        # generate command to execute
        cmd="xrntranslate-cli --json2cmd -json command.json > command.sh"
        echo "$cmd" ; eval "$cmd"
        cmd="chmod 777 command.sh"
        echo "$cmd" ; eval "$cmd"

        # generate command to execute
        cmd="./command.sh"
        echo "$cmd" ; eval "$cmd"

        # check outputs
        cmd="xrnarchive-cli --check-outputs -json-command command.json -json-log $outputjson_file"
        echo "$cmd" ; eval "$cmd"

        # generate server scripts
        cmd="xrnarchive-cli --generate-server-scripts -json-command command.json -archive-outputs-script archive_outputs.sh"
        echo "$cmd" ; eval "$cmd"
        cmd="chmod 777 archive_outputs.sh"
        echo "$cmd" ; eval "$cmd"
        
        # generate output archive
        cmd="./archive_outputs.sh $output_tar_file ."
        echo "$cmd" ; eval "$cmd"

        # change directory to current directory
        cmd="cd $current_dir"
        echo "$cmd" ; eval "$cmd"

        # clean cache
        cmd="rm -rf $cache_dir/*"
        echo "$cmd" ; eval "$cmd"

    fi
done
